package com.plate.common.core.utils;

import org.apache.tika.Tika;
import org.springframework.http.*;

import java.net.URLEncoder;

/**
 * @ClassName HttpUtil
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年12月10日 15:46
 * @Version 1.0
 */

public class HttpUtil {

    public static ResponseEntity<?> getResponseEntity(byte[] fileContent, String contentDispositionType, String originalFileName){
        ResponseEntity.BodyBuilder responseEntity = ResponseEntity.ok();
        HttpHeaders httpHeaders = new HttpHeaders();
        Tika tika = new Tika();
        String mediaType = tika.detect(fileContent);
        httpHeaders.setContentType(MediaType.parseMediaType(mediaType));
        httpHeaders.setContentDisposition(ContentDisposition.builder(contentDispositionType)
                .filename(URLEncoder.encode(originalFileName)).build());//解决文件名中文乱码的问题
        httpHeaders.setCacheControl(CacheControl.noCache());
        return responseEntity.headers(httpHeaders).body(fileContent);
    }

}
